package rabbit.flt.test.mvc;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import rabbit.flt.common.exception.FltException;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RestController
@RequestMapping("/mvc")
public class BootController {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @GetMapping("/hello")
    public String hello() {
        logger.info("invoke hello");
        return "abc";
    }

    @GetMapping("/hello1")
    public String hello1() {
        return "abc";
    }

    @GetMapping("/error")
    public void error() {
        logger.info("已处理的error");
        throw new FltException("error");
    }

    /**
     * 未处理的异常
     */
    @GetMapping("/unHandledError")
    public String unHandledError() {
        throw new RuntimeException("hello");
    }

    /**
     * 直接500
     */
    @GetMapping("/directError")
    public String directError(HttpServletResponse response) {
        response.setStatus(500);
        return "direct500";
    }

    /**
     * 直接500
     */
    @GetMapping("/directError2")
    public void directError2(HttpServletResponse response) throws IOException {
        response.setStatus(500);
        response.getOutputStream().write("out by stream".getBytes());
    }

    /**
     * 直接500
     */
    @GetMapping("/directError3")
    public void directError3(HttpServletResponse response) throws IOException {
        response.setStatus(500);
        response.getWriter().write("out by writer");
    }
}
